
package com.ifcar.app.model;

import com.ifcar.app.db.DBUtils;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Tools;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class News {
    public static List<Map<String, Object>> getListFromDB(SQLiteDatabase db, String columnID, String type) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

        String sql = "";
        if (!"head".equals(type)) {
            type = Tools.equalHead(columnID);
        }

        if (type.equals("head")) {
            sql = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE + " where "
                    + NewsSQLiteOpenHelper.NEWS_COL_ID + "=? limit 0,20";
        } else {
            sql = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE + " where "
                    + NewsSQLiteOpenHelper.NEWS_COL_ID + "=? order by news_ptime desc limit 0,20";
        }

        Cursor cursor = db.rawQuery(sql, new String[] {
            columnID
        });

        if (cursor != null && cursor.getCount() != 0) {

            HashMap<String, Object> map = null;
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                map = new HashMap<String, Object>();
                map.put(GetColumnsInfo.DOCID_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_DOC_ID)));
                map.put(GetColumnsInfo.IMGSRC_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_IMG_SRC)));
                map.put(GetColumnsInfo.DIGEST_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_DIGEST)));
                map.put(GetColumnsInfo.REPLYCOUNT_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_REPLY_COUNT)));
                map.put(GetColumnsInfo.URL_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_URL)));
                map.put(GetColumnsInfo.TITLE_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_TITLE)));
                map.put(GetColumnsInfo.SUBTITLE_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_SUBTITLE)));
                if (!cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME))
                        .equals("")) {
                    map.put(GetColumnsInfo.PTIME_TAG, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME)));
                }
                map.put(GetColumnsInfo.HASIMG_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_IS_HASIMG)));
                map.put(GetColumnsInfo.SPECIAL_INDEX_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_SPECIAL_INDEX)));
                map.put(GetColumnsInfo.SPECIAL_TNAME_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_SPECIAL_TNAME)));
                map.put(GetColumnsInfo.SPECIAL_ID_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_SPECIAL_ID)));

                map.put(GetColumnsInfo.EX_JSON_TAG, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_EXTENDS_JSON)));
                data.add(map);

            }

            cursor.close();
        }

        if(null!=cursor){
            cursor.close();
        }
        
        return data;
    }
    
    public static void insertSpecialListToDB(SQLiteDatabase db, List<Map<String, Object>> data, String columnID){

        String docid = "";
        String imgsrc = "";
        String digest = "";
        String url = "";
        String replyCount = "";
        String title = "";
        String ptime = "";
        String hasimg = "0";
        String specialid = "";
        String sepcialindex = "";
        String sepcialtname = "";
        String extendjson = "";
        db.beginTransaction();
        DBUtils.deleteDataToNewsListTabel(db, columnID);
        
        try {

            
            for(int i=0; i<data.size(); i++) {
                try {
                    if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DOCID_TAG) != null) {
                        docid = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DOCID_TAG));
                        
                        
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.IMGSRC_TAG) != null) {
                            imgsrc = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.IMGSRC_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DIGEST_TAG) != null) {
                            digest = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DIGEST_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.REPLYCOUNT_TAG) != null) {
                            replyCount = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.REPLYCOUNT_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.URL_TAG) != null) {
                            url = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.URL_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.TITLE_TAG) != null) {
                            title = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.TITLE_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.PTIME_TAG) != null) {
                            ptime = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.PTIME_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.HASIMG_TAG) != null) {
                            hasimg = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.HASIMG_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_ID_TAG) != null) {
                            specialid = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_ID_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_INDEX_TAG) != null) {
                            sepcialindex = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_INDEX_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_TNAME_TAG) != null) {
                            sepcialtname = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_TNAME_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.EX_JSON_TAG) != null) {
                            extendjson = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.EX_JSON_TAG));
                        }
                        
                        
                            DBUtils.insertDataToNewsListTable(db, columnID, docid, imgsrc, digest, 
                                    replyCount, url, title, ptime, "", "0", "N",hasimg,
                                    specialid,sepcialindex,sepcialtname,extendjson);

                        
                    }               

                } catch (Exception e) {
                    
                    e.printStackTrace();
                }
            }
            db.setTransactionSuccessful();      
        }catch(Exception e){  
            
               
        }  
        db.endTransaction();

    
    }
    
    public static void insertListToDB(SQLiteDatabase db, List<Map<String, Object>> data, String columnID, String type) {
        String docid = "";
        String imgsrc = "";
        String digest = "";
        String url = "";
        String replyCount = "";
        String title = "";
        String subtitle = "";
        String ptime = "";
        String hasimg = "0";
        String latestTime = "";
        String specialid = "";
        String sepcialindex = "";
        String sepcialtname = "";
        String extendjson = "";
        String s = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE + 
        " where " + NewsSQLiteOpenHelper.NEWS_COL_ID + "='" + columnID + "' order by news_ptime desc limit 0,1";
        Cursor c = db.rawQuery(s, new String[]{});
        if(c != null && c.getCount() > 0 && c.moveToFirst()) latestTime = c.getString(c.getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME));
        if(null!=c){
            c.close();
        }
        //Log.i("latestTime", latestTime);
        db.beginTransaction();  
        if(!"head".equals(type)){
            type = Tools.equalHead(columnID);
        }
        
        if("head".equals(type)){
            db.execSQL("delete from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE
                            + " where news_col_id='" + columnID + "'");
        }
        
        try {
            Cursor cursor = null;
            for(int i=0; i<data.size(); i++) {
                try {
                    if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DOCID_TAG) != null) {
                        docid = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DOCID_TAG));
                        String sql = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE + 
                        " where " + NewsSQLiteOpenHelper.NEWS_DOC_ID + "=? and " + 
                        NewsSQLiteOpenHelper.NEWS_COL_ID + "='" + columnID + "'";
                        cursor = db.rawQuery(sql, new String[]{docid});
                        
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.IMGSRC_TAG) != null) {
                            imgsrc = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.IMGSRC_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DIGEST_TAG) != null) {
                            digest = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.DIGEST_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.REPLYCOUNT_TAG) != null) {
                            replyCount = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.REPLYCOUNT_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.URL_TAG) != null) {
                            url = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.URL_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.TITLE_TAG) != null) {
                            title = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.TITLE_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SUBTITLE_TAG) != null) {
                            subtitle = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SUBTITLE_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.PTIME_TAG) != null) {
                            ptime = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.PTIME_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.HASIMG_TAG) != null) {
                            hasimg = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.HASIMG_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_ID_TAG) != null) {
                            specialid = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_ID_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_INDEX_TAG) != null) {
                            sepcialindex = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_INDEX_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_TNAME_TAG) != null) {
                            sepcialtname = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.SPECIAL_TNAME_TAG));
                        }
                        if(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.EX_JSON_TAG) != null) {
                            extendjson = StringUtil.GetString(((HashMap<String, Object>)data.get(i)).get(GetColumnsInfo.EX_JSON_TAG));
                        }
                        
                        
                        if(cursor == null || cursor.getCount() == 0) {
                            DBUtils.insertDataToNewsListTable(db, columnID, docid, imgsrc, digest, 
                                    replyCount, url, title, ptime, subtitle, "0", "N",hasimg,
                                    specialid,sepcialindex,sepcialtname,extendjson);

                        } else {
                            ContentValues initialValues = new ContentValues();
                            initialValues.put(NewsSQLiteOpenHelper.NEWS_REPLY_COUNT, replyCount);
                            db.update(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, initialValues, "news_doc_id=?", new String[]{docid});
                        }
                        if(i == data.size()-1 && (latestTime.equals("") || StringUtil.compareDate(ptime, latestTime)) && type.equals("nonhead")) {
                           db.execSQL("delete from "+NewsSQLiteOpenHelper.NEWS_LIST_TABLE+" where news_col_id='"+columnID+"' and news_ptime<'"+ptime+"'");
                        }
                        if(null!=cursor){
                            cursor.close();
                        }
                    }               

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    if(null!=cursor){
                        cursor.close();
                    }
                }
            }
            db.setTransactionSuccessful();      
        }catch(Exception e){  
             //当数据库操作出现错误时，需要捕获异常，结束事务  
               db.endTransaction();  
        }  
        db.endTransaction();

    }
    
    public static void insertListToDBForLocal(SQLiteDatabase db, List<HashMap<String, Object>> data, String columnID, String type) {
        String docid = "";
        String imgsrc = "";
        String digest = "";
        String url = "";
        String replyCount = "";
        String title = "";
        String ptime = "";
        String hasimg = "0";
        String specialid = "";
        String sepcialindex = "";
        String sepcialtname = "";
        String extendjson = "";
       
        db.beginTransaction();
        db.execSQL(String.format("DELETE FROM news_list WHERE news_col_id='%s'", columnID));
        try {
            for (int i = 0; i < data.size(); i++) {
                try {
                    if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.DOCID_TAG) != null) {
                        docid = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                GetColumnsInfo.DOCID_TAG));

                        if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.IMGSRC_TAG) != null) {
                            imgsrc = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.IMGSRC_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.DIGEST_TAG) != null) {
                            digest = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.DIGEST_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i))
                                .get(GetColumnsInfo.REPLYCOUNT_TAG) != null) {
                            replyCount = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.REPLYCOUNT_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.URL_TAG) != null) {
                            url = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.URL_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.TITLE_TAG) != null) {
                            title = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.TITLE_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.PTIME_TAG) != null) {
                            ptime = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.PTIME_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i)).get(GetColumnsInfo.HASIMG_TAG) != null) {
                            hasimg = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.HASIMG_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i))
                                .get(GetColumnsInfo.SPECIAL_ID_TAG) != null) {
                            specialid = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.SPECIAL_ID_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i))
                                .get(GetColumnsInfo.SPECIAL_INDEX_TAG) != null) {
                            sepcialindex = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.SPECIAL_INDEX_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i))
                                .get(GetColumnsInfo.SPECIAL_TNAME_TAG) != null) {
                            sepcialtname = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.SPECIAL_TNAME_TAG));
                        }
                        if (((HashMap<String, Object>) data.get(i))
                                .get(GetColumnsInfo.EX_JSON_TAG) != null) {
                            extendjson = StringUtil.GetString(((HashMap<String, Object>) data.get(i)).get(
                                    GetColumnsInfo.EX_JSON_TAG));
                        }
                        DBUtils.insertDataToNewsListTable(db, columnID, docid, imgsrc, digest,
                                replyCount, url, title, ptime, "", "0", "N", hasimg, specialid,
                                sepcialindex, sepcialtname,extendjson);
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            // 当数据库操作出现错误时，需要捕获异常，结束事务
            db.endTransaction();
        }
        db.endTransaction();

    }
}
